package com.ikan.positionserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ikan.positionserver.dto.RackColumnDto;
import com.ikan.positionserver.dto.RackDto;
import com.ikan.positionserver.dto.RoomDto;
import com.ikan.positionserver.entity.DeviceUnit;
import com.ikan.positionserver.entity.Room;
import com.ikan.positionserver.response.JsonReturn;
import com.ikan.positionserver.service.IDeviceUnitService;
import com.ikan.positionserver.service.IRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * room前端控制器  RestController注解 将结果以JSON形式返回
 * </p>
 *
 * @author ikan
 * @since 2024-04-10
 */
@Slf4j
@RestController
@RequestMapping("/room")
@Api(value = "Room", tags = "Room")
public class RoomController {
    @Autowired
    private IRoomService roomService;
    @Autowired
    private IDeviceUnitService deviceUnitService;
    //默认机房
    private static final long DEFAULT_ROOM_ID = 1L;

    /**
     * POST请求方式
     *
     * @param room 保存的对象
     * @return JsonReturn
     */
    @PostMapping
    @ApiOperation(value = "插入记录")
    public JsonReturn insert(Room room) {
        try {
            return JsonReturn.buildSuccess(roomService.save(room) ? room :
                    "room--新增失败");
        } catch (Exception e) {
            log.error("新增失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }

    }

    /**
     * PUT请求方式
     *
     * @param room 要修改的对象
     * @return JsonReturn
     */
    @PutMapping
    @ApiOperation(value = "修改记录")
    public JsonReturn update(Room room) {
        try {
            return JsonReturn.buildSuccess(roomService.updateById(room) ? room :
                    "room--修改失败");
        } catch (Exception e) {
            log.error("修改失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }

    }

    /**
     * 批量删除 DELETE请求方式
     *
     * @param ids Long 类型 List 集合
     * @return JsonReturn
     */
    @DeleteMapping("remove")
    @ApiOperation(value = "删除多条记录")
    public JsonReturn delete(@RequestBody List<Long> ids) {
        try {
            roomService.removeByIds(ids);
            return JsonReturn.buildSuccess(ids, "操作成功");
        } catch (Exception e) {
            log.error("操作失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }

    /**
     * 查询一个 GET请求
     *
     * @param id 查找对象的主键ID
     * @return JsonReturn
     */
    @GetMapping("findOne/{id}")
    @ApiOperation(value = "查询单条记录")
    public JsonReturn findOne(@PathVariable("id") Long id) {
        try {
            Room room = roomService.getById(id);
            return JsonReturn.buildSuccess(room, "操作成功");
        } catch (Exception e) {
            log.error("查询单个失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }

    /**
     * 查询所有 GET
     *
     * @param pageNum  当前页
     * @param pageSize 每页最大数据数
     * @return JsonReturn
     */
    @GetMapping("findAll/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页查询")
    public JsonReturn findAll(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        try {
            Page<Room> page = roomService.page(new Page<Room>(pageNum, pageSize));
            return JsonReturn.buildSuccess(page, "操作成功");
        } catch (Exception e) {
            log.error("分页查询失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }


    @GetMapping("getRoomAllInfo")
    @ApiOperation(value = "获取机房的全部机柜信息")
    public JsonReturn getRoomAllInfo(@RequestParam("name") String name) {
        try {
            RoomDto roomDto = roomService.getRoomAllInfo(name);
            return JsonReturn.buildSuccess(roomDto, "操作成功");
        } catch (Exception e) {
            log.error("查询单个失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }

    @GetMapping("getBriefRoom")
    @ApiOperation(value = "获取机房简要信息")
    public JsonReturn getBriefRoom(@RequestParam("name") String name) {
        try {
            RoomDto roomDto = roomService.getRoomAllInfo(name);
            return JsonReturn.buildSuccess(roomDto, "操作成功");
        } catch (Exception e) {
            log.error("查询单个失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }

    @GetMapping("saveServerUnit")
    @ApiOperation(value = "获取机房的全部机柜信息")
    public JsonReturn saveServerUnit(@RequestParam("name") String name) {
        try {
            RoomDto roomDto = roomService.getRoomAllInfo(name);
            for (RackColumnDto rackColumnDto : roomDto.getRackColumnList()) {
                for (RackDto rackDto : rackColumnDto.getRackDtoList()) {
                    for (int i = 1; i <= 44; i++) {
                        DeviceUnit deviceUnit = new DeviceUnit();
                        deviceUnit.setRackId(rackDto.getId());
                        deviceUnit.setRackName(rackDto.getName());
                        deviceUnit.setSeq(i);
                        deviceUnit.setCreateTime(new Date());
                        deviceUnit.setCreator("system");

                        QueryWrapper<DeviceUnit> deviceUnitQueryWrapper = new QueryWrapper<>();
                        deviceUnitQueryWrapper.eq("rack_id", rackDto.getId());
                        deviceUnitQueryWrapper.eq("seq", i);

                        List<DeviceUnit> serverUnitList = deviceUnitService.list(deviceUnitQueryWrapper);
                        if (CollectionUtils.isEmpty(serverUnitList)) {
                            deviceUnitService.save(deviceUnit);
                        }
                    }
                }
            }
            return JsonReturn.buildSuccess(roomDto, "操作成功");
        } catch (Exception e) {
            log.error("查询单个失败", e);
            return JsonReturn.buildFailure("操作失败" + e.getMessage());
        }
    }

}
